package com.alibaba.citrus.cr.mn.order.center.facade.function.convert;

import com.alibaba.citrus.cr.common.enums.ordercenter.OrderCategoryEnum;
import com.alibaba.citrus.cr.common.enums.ordercenter.OrderChannelEnum;
import com.alibaba.citrus.cr.common.util.DateUtils;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crb2btradecenter.obtcsaleorderindex.model.ObtcSaleOrderIndex;
import com.epoch.app.mnordercenter.model.dto.SalesOrderAtomQueryResponse;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhangxiaorui
 * @ClassName ObtcSaleOrderIndexToSalesOrderAtomQueryResponseConvertor
 * @description: TODO
 * @date 2023/05/19 19:42
 * @version: 1.0
 */
public class SaleOrderIndexToSalesOrderAtomQueryResponseConvertor {

    private static final Log log = Log.getLogger(SaleOrderIndexToSalesOrderAtomQueryResponseConvertor.class);

    public static List<SalesOrderAtomQueryResponse> convertMainOrder(Result<List<ObtcSaleOrderIndex>> listResult) {
        if (Objects.isNull(listResult) || !listResult.isSuccess() || CollectionUtils.isEmpty(listResult.getResult())) {
            return Lists.newArrayList();
        }
        return listResult.getResult().stream()
                .map(SaleOrderIndexToSalesOrderAtomQueryResponseConvertor::convertMainOrder)
                .collect(Collectors.toList());
    }

    @NotNull
    private static SalesOrderAtomQueryResponse convertMainOrder(ObtcSaleOrderIndex order) {
        SalesOrderAtomQueryResponse response = new SalesOrderAtomQueryResponse();
        response.setBusinessChannel(nvlToEmptyStr(order.getMainChannelOutCode()));
        response.setBusinessChannelName(nvlToEmptyStr(order.getMainChannelName()));
        response.setCostCenterCode(nvlToEmptyStr(order.getMainCostCenter()));
        response.setCostCenterName(nvlToEmptyStr(order.getMainCostCenterName()));
        response.setCreateTime(DateUtils.toDateStr(order.getMainGmtCreate(), DateUtils.DATETIME_FORMAT));
        response.setCreatorCode(nvlToEmptyStr(order.getMainCreatorCode()));
        response.setCreatorName(nvlToEmptyStr(order.getMainCreatorName2()));
        response.setCustomerCode(nvlToEmptyStr(order.getMainCustomerCode()));
        response.setCustomerName(nvlToEmptyStr(order.getMainCustomerName()));
        response.setCustomerPurchaseDate(DateUtils.toDateStr(order.getMainCustomerPurchaseDate(), DateUtils.DATE_FORMAT));
        response.setCustomerPurchaseOrderNo(nvlToEmptyStr(order.getMainCustomerReferenceNo()));
        response.setDeliveryMethodCode(nvlToEmptyStr(order.getMainTransportModeCode()));
        response.setDeliveryMethodName(nvlToEmptyStr(order.getMainTransportModeName()));
        response.setExpectedDeliveryDate(DateUtils.toDateStr(order.getMainCustomerExpectDate(), DateUtils.DATE_FORMAT));
        response.setFromSys(nvlToEmptyStr(order.getMainFromSys()));
        response.setModifierCode(nvlToEmptyStr(order.getMainModifierCode()));
        response.setModifierName(nvlToEmptyStr(order.getMainModifierName2()));
        response.setModifierTime(DateUtils.toDateStr(order.getMainGmtModified(), DateUtils.DATETIME_FORMAT));
        response.setNote(nvlToEmptyStr(order.getMainRemark()));
        response.setOrderReasonCode(nvlToEmptyStr(order.getMainCreateReason()));
        response.setOrderReasonName(nvlToEmptyStr(order.getMainCreateReasonName()));
        if (order.getMainStatus() != null) {
            response.setOrderStatus(String.valueOf(order.getMainStatus()));
        } else {
            response.setOrderStatus("");
        }
        response.setOutOrderNo(nvlToEmptyStr(order.getMainOuterOrderId()));
        response.setPreOrderNo(nvlToEmptyStr(order.getMainPreOrderNo()));
        response.setProductGroupCode(nvlToEmptyStr(order.getMainProductGroupCode()));
        response.setProductGroupName(nvlToEmptyStr(order.getMainProductGroupName()));
        response.setSalesChannelCode(nvlToEmptyStr(order.getMainSaleChannelOutCode()));
        response.setSalesChannelName(nvlToEmptyStr(order.getMainSaleChannelName()));
        response.setSalesDepartmentCode(nvlToEmptyStr(order.getMainDepartmentOutCode()));
        response.setSalesDepartmentName(nvlToEmptyStr(order.getMainDepartmentName()));
        response.setSalesOrderNo(nvlToEmptyStr(order.getMainOrderBizId()));
        response.setSalesOrderType(nvlToEmptyStr(order.getMainOrderType()));
        response.setSalesOrderTypeName(nvlToEmptyStr(order.getMainOrderTypeName()));
        response.setSalesOrganizationCode(nvlToEmptyStr(order.getMainOrganizationOutCode()));
        response.setSalesOrganizationName(nvlToEmptyStr(order.getMainOrganizationName()));
        response.setShippingWarehouseCode(nvlToEmptyStr(order.getMainWareHouseOutCode()));
        response.setShippingWarehouseName(nvlToEmptyStr(order.getMainWareHouseName()));
        response.setShipToPartyCode(nvlToEmptyStr(order.getMainReceiveAddressCode()));
        response.setShipToPartyName(nvlToEmptyStr(order.getMainReceiveDetailAddress()));
        response.setSourceOrderNo(nvlToEmptyStr(order.getMainSourceOrderNo()));
        response.setBusinessType(order.getMainBusinessType());
        response.setBusinessTypeName(nvlToEmptyStr(order.getMainBusinessTypeName()));
        response.setBusinessUnitCode(nvlToEmptyStr(order.getMainBisUnitOutCode()));
        response.setBusinessUnitName(nvlToEmptyStr(order.getMainBisUnitName()));
        response.setCashCustomerName(nvlToEmptyStr(order.getMainCashCustomerName()));
        response.setBizFeatures(nvlToEmptyStr(order.getMainBizFeatures()));

        //一盘货修改
        response.setOrderCategoryCode(nvlToEmptyStr(order.getMainOrderCategoryCode()));
        OrderCategoryEnum orderCategoryEnum = OrderCategoryEnum.of(order.getMainOrderCategoryCode());
        if (orderCategoryEnum != null) {
            response.setOrderCategoryName(orderCategoryEnum.getDesc());
        } else {
            response.setOrderCategoryName("");
        }
        response.setCreatorName(nvlToEmptyStr(order.getMainCreatorName()));
        response.setModifierCode(nvlToEmptyStr(order.getMainModifierCode()));
        response.setModifierName(nvlToEmptyStr(order.getMainModifierName2()));
        response.setRelatonNo(nvlToEmptyStr(order.getMainRelationNo()));
        if (order.getMainStatus() != null) {
            response.setOrderStatus(String.valueOf(order.getMainStatus()));
        } else {
            response.setOrderStatus("");
        }
        response.setOrderChannelCode(nvlToEmptyStr(order.getMainOrderChannelCode()));
        OrderChannelEnum orderChannelEnum = OrderChannelEnum.of(order.getMainOrderChannelCode());
        if (orderChannelEnum != null) {
            response.setOrderChannelName(orderChannelEnum.getDesc());
        }
        response.setReceiveWarehouseCode(nvlToEmptyStr(order.getMainReceiveWareHouseOutCode()));
        response.setReceiveWarehouseName(nvlToEmptyStr(order.getMainReceiveWarehouseName()));
        response.setReceiveFactoryCode(nvlToEmptyStr(order.getMainReceiveFactoryCode()));
        response.setReceiveFactoryName(nvlToEmptyStr(order.getMainReceiveFactoryName()));
        response.setReceiveStockCode(nvlToEmptyStr(order.getMainReceiveAddressCode()));
        response.setReceiveStockName(nvlToEmptyStr(order.getMainReceiveDetailAddress()));
        response.setShippingWarehouseCode(nvlToEmptyStr(order.getMainWareHouseCode()));
        response.setShippingWarehouseName(nvlToEmptyStr(order.getMainWareHouseName()));
        response.setDeliveryFactoryCode(nvlToEmptyStr(order.getMainDeliveryFactoryCode()));
        response.setDeliveryFactoryName(nvlToEmptyStr(order.getMainDeliveryFactoryName()));
        response.setStockCode(nvlToEmptyStr(order.getMainStockCode()));
        response.setStockName(nvlToEmptyStr(order.getMainStockName()));
        response.setCustomerGrade(nvlToEmptyStr(order.getMainCustomerGrade()));
        response.setCustomerGroup(nvlToEmptyStr(order.getMainCustomerGroup()));
        response.setCustomerGroupName(nvlToEmptyStr(order.getMainCustomerGroupName()));
        response.setFulfillmentTypeCode(nvlToEmptyStr(order.getMainFulfillmentTypeCode()));
        response.setFulfillmentPriority(nvlToEmptyStr(order.getMainFulfillmentPriority()));
        response.setAutoFulfillment(nvlToEmptyStr(order.getMainAutoFulfillment()));
        response.setSourceTime(nvlToEmptyStr(order.getMainSourceDate()));
        if (StringUtils.isNotBlank(order.getMainOrderBizTag())) {
            String[] orderBizTagArray = order.getMainOrderBizTag().split(",");
            response.setOrderBizTag(Arrays.asList(orderBizTagArray));
        } else {
            response.setOrderBizTag(Lists.newArrayList());
        }
        response.setKaSystem(nvlToEmptyStr(order.getMainKaSystem()));
        response.setKaArea(nvlToEmptyStr(order.getMainKaArea()));
        response.setGroupCallbackMsg(nvlToEmptyStr(order.getMainGroupCallbackMsg()));
        return response;
    }

    /**
     * 字段值如果为空，返回空字符串，而不是null。
     */
    private static String nvlToEmptyStr(String source) {
        if (org.apache.commons.lang.StringUtils.isBlank(source)) {
            return "";
        }
        return source;
    }
}
